<%-- 
    Document   : artifactList
    Created on : 13.5.2014, 20:24:26
    Author     : Odin
--%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<c:set var="extension" value="${artifact.substring(artifact.lastIndexOf('.')+1).toLowerCase()}" target="String" />
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Zobrazeni artefaktu</title>
        <script type="text/javascript" src="assets/js/jquery-1.11.1.min.js"></script>
        <script type="text/javascript" src="assets/js/jquery-ui-1.10.4.min.js"></script>
        <script type="text/javascript">
            $(function() {
                var ajaxComplete = 0;
                $(".metadata").each(function (){
                    var artifactName = $(this).children(".artifact").html();
                    var version = $(this).children(".version_number").html();
                    var current = $(this);
                    $.ajax({
                        url: "/metadata/${format}",
                        data: {artifact: artifactName, version: version}
                    }).done(function(html) {
                        current.html(html);
                        ajaxComplete++;
                        if(ajaxComplete == $(".version").length) {
                            if($(".version").length > 1) makeVersionBrowsingFancy();
                        }
                    });
                });
                
                var vNum = $(".version").length;
                var vDisplayed = vNum;
                if(window.location.href.indexOf("#") != -1
                        && parseInt(window.location.hash.substr(1)) <= vNum) {
                    vDisplayed = window.location.hash.substr(1);
                }
                function makeVersionBrowsingFancy() {
                    $(".version").css("width","76%").css("position","absolute").css("left","12%").css("top","0");
                    $(".versions")
                        .prepend('<div class="previous"></div>')
                        .append('<div class="next"></div>');
                    $(".previous").click(prev);
                    $(".next").click(next);
                    $(document).keydown(function(e) {
                        if(e.keyCode == 37) prev(); // left arrow key
                        if(e.keyCode == 39) next(); // right arrow key
                    })
                    $(".version").hide();
                    $($(".version")[vNum - vDisplayed]).show();
                    refreshVersionNavigation();
                }
                function prev() {
                    if(vDisplayed <= 1) return false;
                    $($(".version")[vNum - vDisplayed]).hide("slide", {direction: "right", duration: "slow"});
                    vDisplayed--;
                    $($(".version")[vNum - vDisplayed]).show("slide", {direction: "left", duration: "slow"});
                    refreshVersionNavigation();
                }
                function next() {
                    if(vDisplayed >= $(".version").length) return false;
                    $($(".version")[vNum - vDisplayed]).hide("slide", {direction: "left", duration: "slow"});
                    vDisplayed++;
                    $($(".version")[vNum - vDisplayed]).show("slide", {direction: "right", duration: "slow"});
                    refreshVersionNavigation();
                }
                function refreshVersionNavigation() {
                    if(vDisplayed > 1) {
                        $(".previous").html('Předchozí verze<br>(číslo '+ (vDisplayed-1) +')<br>&lt;&lt;').show();
                    }
                    else $(".previous").hide();
                    
                    if(vDisplayed < $(".version").length) {
                        $(".next").html('Další verze<br>(číslo '+ (vDisplayed+1) +')<br>&gt;&gt;').show();
                    }
                    else $(".next").hide();
                }
                window.onhashchange = function() {
                    if(window.location.href.indexOf("#") != -1) {
                        if(parseInt(window.location.hash.substr(1)) > vNum) vDisplayed = vNum;
                        else vDisplayed = window.location.hash.substr(1);
                        $(".version").hide();
                        $($(".version")[vNum - vDisplayed]).show();
                    }
                }
            });
        </script>
        <style>
            .metadata {
                background: #EEE;
                border: 1px solid Black;
                float: left;
                border-radius: 10px;
            }
            .versions {
                position: relative;
            }
            .previous, .next {
                cursor: pointer;
                padding: 10px;
                max-width: 9%;
                background: #DDF;
                color: #00C;
                border-radius: 15px;
            }
            .previous:hover, .next:hover {
                text-decoration: underline;
            }
            .previous {
                position: absolute;
                top: 20px;
                left: 0;
            }
            .next {
                position: absolute;
                top: 20px;
                right: 0;
                text-align: right;
            }
        </style>
    </head>
    <body>
        <a href="${pageContext.request.contextPath}/listing">&lt; Zpět na seznam artefaktů</a><br>
<c:choose>
    <c:when test="${artifact == null}">
        <h1>
        <c:if test="${param.artifact != null}">
            <a href="${pageContext.request.contextPath}/show?artifact=${param.artifact}"></c:if>
                Tato stránka je zobrazitelná pouze prostřednictvím servletu
        <c:if test="${param.artifact != null}"></a></c:if>
        </h1>
    </c:when>
    <c:otherwise>
        <h1>Zobrazení artefaktu</h1>
        <h2>${artifact}</h2>
        
        <h3>Počet verzí: ${versionsCount}</h3>
        
        <h4>Uložené verze ke stažení:</h4>
        <div class="versions">
        <c:forEach var="i" begin="1" end="${versionsCount}">
            <c:set var="version" value="${versionsCount - i + 1}" />
            <div class="version">
                <div class="version_header">
                    <a href="${pageContext.request.contextPath}/download?artifact=${artifact}&amp;version=${version}">
                        <strong>Verze ${version}</strong></a>
                        - metadata verze:<br>
                </div>
                <div class="metadata">
                    <span class="artifact">${artifact}</span>
                    <span class="version_number">${version}</span>
                </div>
            </div>
            <br>
            <br>
        </c:forEach>
        </div>
    </c:otherwise>
</c:choose>
    </body>
</html>
